import buildingApi from '@/config/server/api/building'
import dt from '@dt/dt'
import tool from '@/common/tool.js'

function listByHousesId(housesId) {
  return buildingApi.product.listByHousesId
    .fetch({
      housesId
    })
    .then((res) => {
      return res.map((item) => {
        item.key = item.productId.toString()
        item.value = item.productName
        return item
      })
    })
}

let products = [],
  productsTree = []

function getProduct(hid) {
  if (!hid && tool.getStorage('isbuildFlag') == 'buildpage') {
    hid = dt.buildInfo().housesId
  }
  if (hid) {
    return listByHousesId(hid).then((res) => {
      products = res
      return res
    })
  }
  return Promise.resolve(products)
}

function create(opt = {}) {
  // key data multiple hid
  return {
    title: opt.title || '产品',
    placeholder: opt.placeholder || '请选择产品',
    key: opt.key,
    disabled: opt.disabled,
    load: (o) => {
      getProduct(opt.hid).then((res) => {
        o.select.options = res
      })
    },
    select: {
      options: products,
      multiple: opt.multiple,
      maxTagCount: opt.maxTagCount
    },
    data: opt.data,
    rules: [
      {
        required: true,
        type: opt.type,
        message: '请选择产品',
        trigger: 'blur'
      }
    ]
  }
}

function listByHousesIdTree(housesId, productIds) {
  return buildingApi.product.listByHousesId
    .fetch({
      housesId
    })
    .then((res) => {
      let parentIds = [],
        children = []
      let parentArr = [
        {
          title: '',
          expand: true,
          id: '',
          children: []
        }
      ]
      res.map((item) => {
        item.id = item.productId
        item.title = item.productName
        parentIds.push(item.productId.toString())
        if (productIds && productIds.includes(item.id)) {
          item.checked = true
        }
        children.push(item)
      })
      if (res.length) {
        parentArr[0].title = `全部产品，共${res.length}个`
        parentArr[0].id = parentIds.join(',') + 1
        parentArr[0].children = children
      } else {
        parentArr = []
      }
      return parentArr
    })
}

function getProductTree(hid, productIds) {
  if (!hid && tool.getStorage('isbuildFlag') == 'buildpage') {
    hid = dt.buildInfo().housesId
  }
  if (hid) {
    return listByHousesIdTree(hid, productIds).then((res) => {
      products = res
      console.log(res, 'resres')
      return res
    })
  }
  return Promise.resolve(products)
}

function createSelectTree(opt = {}) {
  // key data multiple hid
  console.log(opt)
  return {
    title: opt.title,
    key: opt.key,
    load: (o) => {
      getProductTree(opt.hid, opt.productIds).then((res) => {
        o.tree.options = res
      })
    },
    tree: {
      options: products,
      selectPlaceholder: '请选择产品',
      maxTagCount: 3,
      disabled: opt.disabled
    },
    data: opt.data,
    rules: [
      {
        required: opt.noRequired ? false : true,
        type: 'array',
        message: '请选择产品',
        trigger: 'change'
      }
    ]
  }
}

export default {
  create,
  createSelectTree,
  listByHousesIdTree
}
